package com.hand.wfive.loginAndVip.ui

import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.hand.wfive.R
import com.hand.wfive.base.BaseActivity
import com.hand.wfive.loginAndVip.ApiConfig
import com.hand.wfive.loginAndVip.LoginConfig
import com.hand.wfive.loginAndVip.UserManager
import com.hand.wfive.loginAndVip.VipConfig
import com.hand.wfive.loginAndVip.model.AdConfigModel
import com.hand.wfive.util.click.NoFastClickUtils
import com.hand.wfive.util.click.singleClick
import com.qmuiteam.qmui.widget.dialog.QMUIDialog.MessageDialogBuilder
import com.rxjava.rxlife.RxLife
import kotlinx.android.synthetic.main.login_activity_userinfo.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import rxhttp.wrapper.param.RxHttp

class UserActivity : BaseActivity() {

    private var turnVip: ActivityResultLauncher<Intent>? = null

    override fun getContentViewId() = R.layout.login_activity_userinfo

    override fun init() {
        topBar.setTitle("个人中心")
        topBar.addLeftBackImageButton().singleClick { finish() }

        // Vip页面回调注册
        turnVip = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == Activity.RESULT_OK) {
                updateVipInfo()
            }
        }
        // 用户信息
        updateUserInfo()
        // vip信息
        updateVipInfo()
    }

    private fun updateUserInfo() {
        val user = UserManager.getInstance().curUser
        if (UserManager.LoginType_normal == user.loginType) {
            username.text = user.username
            nick.text = user.username
            changePassword.visibility = View.VISIBLE
        } else {
            username.text = user.nickName
            nick.text = user.nickName
            changePassword.visibility = View.GONE
        }
    }

    private fun updateVipInfo() {
        val user = UserManager.getInstance().curUser
        if (UserManager.getInstance().isVip) {
            continueVip.visibility = View.GONE
            vipState.text = "已开通会员"
            vipType.text = VipConfig.getVipLableByValue(user.vipType)
            if (VipConfig.FOREVER_VIP_TYPE == user.vipType) {
                vipAvailableDays.text = "长期"
            } else {
                if (TextUtils.isEmpty(user.vipDay)) {
                    limitDayLayout.visibility = View.GONE
                } else {
                    limitDayLayout.visibility = View.VISIBLE
                    vipAvailableDays.text = user.vipDay
                }
            }
        } else {
            continueVip.visibility = View.VISIBLE
            vipState.text = "未开通会员"
            vipType.text = "普通用户"
            vipAvailableDays.text = "0"
        }
    }

    fun userBtnClick(view: View) {
        if (NoFastClickUtils.isFastClick()) return
        when (view) {
            changePassword -> startActivity<ChangePasswordActivity>()
            continueVip -> turnVip?.launch(Intent(this, VipCenterActivity::class.java))
            loginOut -> {
                UserManager.getInstance().logOut()
                toast("退出登录成功")
                finish()
            }
            delAccount -> showDelAccountDialog()
        }
    }

    private fun showDelAccountDialog() {
        MessageDialogBuilder(mContext)
            .setTitle("1.注销账号不是退出登录，注销后不可恢复。\n2.当您选择注销账号时，您的所有私有数据包括会员权益、将被不可恢复\n3.因您注销账号造成的会员权益损失，由用户个人承担。")
            .addAction("取消") { dialog, _ -> dialog.dismiss() }
            .addAction("注销") { dialog, _ ->
                dialog.dismiss()
                delAccount()
            }.show()
    }

    private fun delAccount() {
        showLoading()
        RxHttp.postForm(ApiConfig.delAccount)
            .add("username", UserManager.getInstance().curUser.username)
            .add("appid", LoginConfig.AppId)
            .add("password", UserManager.getInstance().curUser.password)
            .asClass(AdConfigModel::class.java)
            .to(RxLife.toMain(this))
            .subscribe({ apiModel ->
                hideLoading()
                if (apiModel.code == ApiConfig.SUCCESSCODE) {
                    UserManager.getInstance().logOut()
                    toast("注销成功")
                    finish()
                } else {
                    showNormalTip(topBar, "注销失败，请重试")
                }
            }, {
                hideLoading()
                showNormalTip(topBar, "注销失败，请重试")
            })
    }

}
